package com.tensquare.qa.controller;
import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.qa.client.LabelClient;
import com.tensquare.qa.pojo.Problem;
import com.tensquare.qa.service.ProblemService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import util.JwtUtil;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/problem")
@CrossOrigin
public class ProblemController {
   @Autowired
   private  ProblemService problemService;
    @Autowired
    private LabelClient labelClient;

    @GetMapping("/label")
    public Result findAllByLabel(){
        Result result = labelClient.findAll();
        return result;
    }

    @PostMapping
    public Result save(@RequestBody Problem problem){
        problemService.save(problem);
        return new Result(true, StatusCode.OK,"ok");
    }
    //  GET/problem 标签全部列表
    @GetMapping
    public Result findAll(){
        List<Problem>problems=problemService.findAll();
        return new Result(true,StatusCode.OK,"ok",problems);
    }
    // GET/problem/{problemId}根据ID查询
    @GetMapping("/{problemId}")
    public Result findByProblemId(@PathVariable String problemId) {
        Problem problem=problemService.findById(problemId);
        return new Result(true,StatusCode.OK,"ok",problem);
    }
    // PUT/problem/{problemId}修改标签
    @PutMapping("{problemId}")
    public Result findByProblemId(@PathVariable String problemId,@RequestBody Problem problem) {
        problem.setId(problemId);
        problemService.updateByProblemId(problem);
        return new Result(true,StatusCode.OK,"ok");
    }
    //  DELETE/problem/{problemId}根据ID删除
    @DeleteMapping("{problemId}")
    public  Result deleteByProblemId(@PathVariable String problemId){
        problemService.deleteByProblemId(problemId);
        return new Result(true,StatusCode.OK,"ok");
    }
    /*
    * POST
    /problem/search/{page}/{size}
    标签分页*/
    @PostMapping("/search/{page}/{size}")
    public Result findByPage(@PathVariable Integer page,
                             @PathVariable Integer size,
                             @RequestBody Map<String,Object> map
    ){
        Page<Problem> pageData=problemService.findByPage(map,page,size);
        PageResult<Problem> pageResult=new PageResult<>(pageData.getTotal(),pageData.getRecords());
        return new Result(true, StatusCode.OK,"ok",pageResult);
    }
    /*
    * POST
    /problem/search
    条件查询*/
    @PostMapping("/search")
    public Result findBySearch( @RequestBody Map<String,Object> map){
        List<Problem> list=problemService.findBySearch(map);
        return new Result(true,StatusCode.OK,"ok",list);
    }

    /*
    *  GET/problem/newlist/{label}/{page}/{size}
        最新问答列表
        * 标签ID 如果此值为0则查询全部标签*/
    @GetMapping("/newlist/{label}/{page}/{size}")
    public  Result newList(@PathVariable String label,
                           @PathVariable Integer page,
                           @PathVariable Integer size){

        Page pageData=problemService.newList(label,page,size);
        PageResult pageResult=new PageResult(pageData.getTotal(),pageData.getRecords());
        return new Result(true, StatusCode.OK,"ok",pageResult);
    }
    /*
    * GET/problem/hotlist/{label}/{page}/{size}
热门问答列表*/
    @GetMapping("/hotlist/{label}/{page}/{size}")
    public  Result hotlist(@PathVariable String label,
                           @PathVariable Integer page,
                           @PathVariable Integer size){
        Page pageData=problemService.hotlist(label,page,size);
        PageResult pageResult=new PageResult(pageData.getTotal(),pageData.getRecords());
        return new Result(true, StatusCode.OK,"ok",pageResult);
    }

    @GetMapping("/waitlist/{label}/{page}/{size}")
    public  Result waitlist(@PathVariable String label,
                           @PathVariable Integer page,
                           @PathVariable Integer size){
        Page pageData=problemService.waitlist(label,page,size);
        PageResult pageResult=new PageResult(pageData.getTotal(),pageData.getRecords());
        return new Result(true, StatusCode.OK,"ok",pageResult);
    }
}
